<template>
  <el-card>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/teaWelcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>成绩管理</el-breadcrumb-item>
      <el-breadcrumb-item>录入成绩</el-breadcrumb-item>
    </el-breadcrumb>
        <el-form :inline="true" :model="teacher" class="demo-form-inline" style="margin-left: -80px;
    margin-top: 10px;" label-width="auto">
          <el-form-item label="工号：">
            <el-input v-model="teacher.teaId" placeholder="工号" disabled></el-input>
          </el-form-item>
          <el-form-item label="姓名：">
            <el-input v-model="teacher.teaName" placeholder="姓名" disabled></el-input>
          </el-form-item>
          <el-form-item label="学院：">
            <el-input v-model="teacher.teaAcademy" placeholder="学院" disabled></el-input>
          </el-form-item>
        </el-form>
    <el-row :gutter="10">
      <!--搜索框-->
      <el-col :span="4">
        <el-input placeholder="课程" v-model="couName" class="input-with-select" style="margin-left: 38px;" readonly>
        </el-input>
      </el-col>
      <el-col :span="4" style="margin-left: 90px;margin-right: 50px;">
        <el-select v-model="couClass" filterable placeholder="请选择班级">
          <el-option label="计科1901班" value="计科1901班"></el-option>
          <el-option label="计科1902班" value="计科1902班"></el-option>
          <el-option label="计科1903班" value="计科1903班"></el-option>
          <el-option label="数媒1901班" value="数媒1901班"></el-option>
          <el-option label="数媒1902班" value="数媒1902班"></el-option>
          <el-option label="软件1901班" value="软件1901班"></el-option>
          <el-option label="软件1902班" value="软件1902班"></el-option>
          <el-option label="电气1901班" value="电气1901班"></el-option>
          <el-option label="电气1902班" value="电气1902班"></el-option>
        </el-select>
      </el-col>
      <el-col :span="1">
        <el-button  type="primary" @click="searchdata">查询</el-button>
      </el-col>
    </el-row>
    <!--表格模块-->
    <el-table :data="students" highlight-current-row
              ref='table'  border stripe height="300" style="margin-top: 30px;" v-if="visible" >
      <el-table-column prop="stuNum" label="学号" fixed align="center" width="120"></el-table-column>
      <el-table-column prop="stuName" label="姓名" align="center"></el-table-column>
      <el-table-column prop="stuSex" label="性别" align="center"></el-table-column>
      <el-table-column prop="stuCard" label="身份证号" align="center" width="180"> </el-table-column>
      <el-table-column prop="stuPhone" label="手机号码" align="center" width="120"> </el-table-column>
      <el-table-column prop="stuAcademy" label="学院" align="center" width="180"></el-table-column>
      <el-table-column prop="stuMajor" label="专业" align="center" width="180"></el-table-column>
      <el-table-column prop="stuClass" label="班级" align="center" width="100"> </el-table-column>
      <el-table-column prop="stuPwd" label="密码" align="center"> </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="100">
        <template slot-scope="scope">
          <el-tag
            type="primary"
            effect="dark"
            @click="openupdate(scope.row)"
          >录入成绩
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
    <!--分页插件-->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="curpage"
      :page-sizes="[2, 4, 8, 10]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next"
      :total="total" v-if="visible"
    ></el-pagination>
    <!-- 弹出的添加模态框 -->
    <el-dialog title="录入成绩" :visible.sync="addstudentdialog" @close="clearuseradd">
      <el-form :model="addstudentform"  ref="addform" label-width="auto"
               style="width:85%"
               class="demo-form-inline form">
        <el-form-item prop="gradeYear" label="学年" >
          <el-select v-model="addstudentform.gradeYear"  filterable style="width: 100%;">
            <el-option label="2020-2021" value="2020-2021"></el-option>
            <el-option label="2021-2022" value="2021-2022"></el-option>
            <el-option label="2022-2023" value="2022-2023"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="gradeTerm" label="学期" >
          <el-select v-model="addstudentform.gradeTerm"  filterable style="width: 100%;">
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="stuNum"  label="学号">
          <el-input v-model="addstudentform.stuNum" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item prop="couId" label="课程号" >
          <el-input v-model="addstudentform.couId" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item prop="couName" label="课程名称" >
          <el-input v-model="addstudentform.couName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="课程类型" prop="couNature">
          <el-select v-model="addstudentform.couNature"  filterable style="width: 100%;">
            <el-option label="必修课" value="必修课"></el-option>
            <el-option label="通识任选课" value="通识任选课"></el-option>
            <el-option label="选修课" value="选修课"></el-option>
            <el-option label="必修项目" value="必修项目"></el-option>
            <el-option label="英语拓展课" value="英语拓展课"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="couCredit" label="学分">
          <el-input v-model="addstudentform.couCredit" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item prop="grade" label="成绩" >
          <el-input v-model="addstudentform.grade" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item prop="gradePoint" label="绩点" >
          <el-input  v-model="addstudentform.gradePoint" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item prop="couCollege" label="开课学院" >
          <el-input  v-model="addstudentform.couCollege" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addstudentdialog = false">取 消</el-button>
        <el-button type="primary" @click="addGrade">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>

</template>

<script>
  import axios from 'axios';//axios
  import Qs from 'qs';//qs
  export default {
    name:"insertGrade",
    //绑定数据
    data() {
      return {
        arr:[],
        // 默认不显示表格
        visible:false,
        students:[],
        curpage:1,//当前页
        pagesize:4,//每页显示条数
        total:0,//总条数
        teacher:{
          teaId:"",
          teaName:"",
          teaAcademy:"",
        },
        // 课程信息
        couName:"",
        // 选择录入班级
        couClass:"",
        addstudentdialog:false,//默认时关闭模态框的
        addstudentform:{
          gradeYear: '',
          gradeTerm: '',
          stuNum: '',
          couId: '',
          couName: '',
          couNature: '',
          couCredit: '',
          grade: '',
          gradePoint:"",
          couCollege:'',
        },//form表单中的数据绑定
      }
    },
    //文档加载事件,当没有任何按钮去点击触发事件时，可以使用该函数在加载时，触发事件！
    mounted() {
      // 打开添加学生的模态框
      // this.studentList();
      this.selectOne();
      this.selectCourse();
    },
    methods:{
      openupdate(data){
        console.log(data)
        //打开模态框
        this.addstudentdialog=true;
        //数据回显
        this.addstudentform = data;
      },
      selectRow(row) { // 给操作行 高亮
        if (row) {
          this.$refs.table.setCurrentRow(row, true);  // 高亮某一行
        } else {
          this.$refs.table.setCurrentRow();  // 取消高亮
        }
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      //添加成绩
      addGrade() {
        var addInfo = this;
        //判断当前校验是否通过
        this.$refs.addform.validate((valid) => {
          if(valid) {
            //发送axios请求
            axios({
              headers:{'Content-Type':'application/x-www-form-urlencoded'},
              method:"post",
              url:"http://localhost:1313/grade/gradeInsert",
              data:Qs.stringify(addInfo.addstudentform)
            }).then((res)=>{
              console.log(res)
              if (res.data.code == 200) {
                //1.提示消息，是否录入成功
                this.$message({
                  message: res.data.msg,
                  type:"success",
                });
                //2.关闭模态框
                this.addstudentdialog=false;
                //3.刷新页面<==>再次执行studentList
                // this.studentList();
              } else {
                this.$message.error(res.data.msg)
              }
            })
          }
        });
      },
      //关闭添加学生的模态框
      clearuseradd() {
        // 重置表单内容
        this.$refs.addform.resetFields();
      },
      // 获取教师信息
      selectOne(){
        let teaId = localStorage.getItem("teaId");
        // 发起GET请求，获取信息
        axios.get(
          "http://localhost:1313/teacher/selectOne",
          {params:{teaId:teaId}}
        ).then((res)=>{
          console.log(res);
          if(res.data.code == 200){
            // 将返回的数据赋值给teacher表单
            this.teacher.teaId = res.data.data.teaId;
            this.teacher.teaName = res.data.data.teaName;
            this.teacher.teaAcademy = res.data.data.teaAcademy;
          }else{
            this.$message.error(res.data.msg)
          }
        })
      },
      // 获取课程信息
      selectCourse(){
        let teaId = localStorage.getItem("teaId");
        // 发起GET请求，获取信息
        axios.get(
          "http://localhost:1313/course/selectCourse",
          {params:{teaId:teaId}}
        ).then((res)=>{
          console.log(res);
          if(res.data.code == 200){
            // 将返回的数据赋值给teacher表单
            this.couName = res.data.data.couName;
            // 将课程信息赋值给录入成绩的表单
            this.addstudentform.couId = res.data.data.couId;
            this.addstudentform.couName = res.data.data.couName;
            this.addstudentform.couCredit = res.data.data.couCredit;
            this.addstudentform.couNature = res.data.data.couNature;
            this.addstudentform.couCollege = res.data.data.couCollege;
          }else{
            this.$message.error(res.data.msg)
          }
        })
      },
      // 查询指定班级学生
      searchdata(){
        // 显示表格
        this.visible = true;
        var students=this;
        axios.get(
          "http://localhost:1313/student/queryStudentByStuClassPage",
          {params:{current:students.curpage, rows: students.pagesize,search:students.couClass}}
        ).then((res)=>{
          console.log(res)
          //将返回数据赋值给绑定数据，完成数据渲染
          this.students = res.data.data.students;
          this.total = res.data.data.total;
        });
      },
      // 监听 pageSize 改变的事件
      handleSizeChange (newSize) {
        //   console.log(newSize)
        //  将监听接受到的每页显示多少条的数据 newSzie 赋值给 pagesize
        this.pagesize = newSize;
        //  修改完以后，重新发起请求获取一次数据
        this.searchdata();
      },
      // 监听 页码值  改变的事件
      handleCurrentChange (newPage) {
        //   console.log(newPage)
        //  将监听接受到的页码值的数据 newPage 赋值给 pagenum
        this.curpage = newPage;
        //  修改完以后，重新发起请求获取一次数据
        this.searchdata();
      },
    },
  }

</script>

<style>
  /* 用来设置当前页面element全局table 选中某行时的背景色*/
  .el-table__body tr.current-row>td{
    background-color: rgba(227, 189, 241, 0.76) !important;
    color: #fff;
  }
  .el-input--suffix .el-input__inner {
    padding-right: 15px;
  }
  .form{
    display: grid;
    justify-content: center;
    /* align-items: center; */
    justify-items: center;
  }

  .el-button--info.is-plain {
    color: #704141;
    background: #f4f4f5;
    border-color: #d3d4d6;
  }
  .el-button--mini {
    font-size: 14px;
    border-radius: 3px;
  }
  .el-breadcrumb{
    padding-bottom: 20px;
  }
  .el-pagination{
    padding-top: 20px;
  }
</style>
